<template>

  <el-row style="width:100%">
    <el-col :span="20">{{ treeNode.name }}</el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col :span="10">
          {{ treeNode.manager }}
        </el-col>
        <el-col :span="10">
          <el-dropdown>
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="add(treeNode)">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="onRemove(treeNode)">删除子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="onEdit(treeNode)">编辑子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>

    </el-col>

  </el-row>

</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 添加
    add() {
      this.$emit('addDepts', this.treeNode)
      // this.$emit('update:showDialog', true)
    },
    // 删除
    async onRemove() {
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        // 发送请求
        await delDepartments(this.treeNode.id)
        // 消息提示
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 更新视图
        this.$emit('deleteDepts')
      } catch (error) {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
        console.log(error)
      }
    },
    // 编辑
    onEdit(treeNode) {
      this.$emit('showEdit', treeNode)
    }
  }
}
</script>

<style>

</style>
